package me.tuyou.jvm.oom;

import java.util.LinkedList;
import java.util.List;

/**
 * 报错：Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
 * -Xss1m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=~/work/NativeThread.hprof
 */
public class NativeThreadOutOfMemory {

    private static void dontStop() {
        int index = 0;
        List<Integer> temp = new LinkedList<>();
        while (true) {
            temp.add(index++);
        }
    }

    public static void stackLeakByThread() {
        while (true) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    dontStop();
                }
            };
            t.start();
        }
    }

    public static void main(String[] args) {
        stackLeakByThread();
    }
}
